package com.by.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.by.bean.UserInfo;
import com.by.context.UserContext;
import com.by.core.utils.Assert;
import com.by.entity.*;
import com.by.mapper.*;
import com.by.msg.Constant;
import com.by.service.SpuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.atomic.AtomicInteger;


@Service
@Slf4j
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements SpuService {
    @Autowired
    SpuMapper spuMapper;
    @Autowired
    SpuDetailMapper spuDetailMapper;
    @Autowired
    SkuMapper skuMapper;
    @Autowired
    SkuStockMapper skuStockMapper;
    @Autowired
    SpuExtensionMapper spuExtensionMapper;
    @Autowired
    AttrMapper attrMapper;
    @Autowired
    AttrValueMapper attrValueMapper;
    @Autowired
    SpuAttrValueMapper spuAttrValueMapper;
    @Autowired
    SpuSkuAttrValueMapper spuSkuAttrValueMapper;

    @Transactional
    @Override
    public boolean save(Spu spu) {
        //获取用户信息
        UserInfo userInfo = UserContext.get();
        UserContext.clear();
        //保存spu
        spu.setShopId(userInfo.getShopId());
        spu.setShopCategoryId(userInfo.getShopCategoryId());
        spu.setUserId(userInfo.getId());
        spu.setUserName(userInfo.getUsername());
        int rs = spuMapper.insert(spu);
        Assert.isZero(rs, Constant.SAVE_SPU);

        //保存spu明细
        SpuDetail spuDetail = new SpuDetail()
                .setSpuId(spu.getSpuId())
                .setDetail(spu.getDetail());
        rs = spuDetailMapper.insert(spuDetail);
        Assert.isZero(rs, Constant.SAVE_SPU_DETAIL);

        AtomicInteger total = new AtomicInteger(0); // 使用 AtomicInteger 替代 int

        //保存sku
        if (null != spu.getSkuList() && spu.getSkuList().size() > 0) {
            //遍历
            spu.getSkuList().forEach(sku -> {
                sku.setSpuId(spu.getSpuId());
                int tmp = skuMapper.insert(sku);
                Assert.isZero(tmp, Constant.SAVE_SKU);
                //保存sku库存
                SkuStock skuStock = new SkuStock()
                        .setSkuId(sku.getSkuId())
                        .setStock(sku.getStock())
                        .setActualStock(sku.getStock());
                tmp = skuStockMapper.insert(skuStock);
                Assert.isZero(tmp, Constant.SAVE_SKU_STOCK);
                total.addAndGet(sku.getStock()); // 原子操作累加
            });
        }
        // 获取最终结果
        int finalTotal = total.get();
        //保存spu库存
        SpuExtension spuExtension = new SpuExtension()
                .setSpuId(spu.getSpuId())
                .setActualStock(finalTotal)
                .setStock(finalTotal);
        rs = spuExtensionMapper.insert(spuExtension);
        Assert.isZero(rs, Constant.SAVE_SPU_EXTENSION);
        //保存规格 获取规格id
        if (null != spu.getAttrs() && spu.getAttrs().size() > 0) {
            //遍历
            spu.getAttrs().forEach(attr -> {
                attr.setShopId(spu.getShopId());
                int tmp = attrMapper.insert(attr);
                Assert.isZero(tmp, Constant.SAVE_ATTR);
                //遍历保存规格值
                //保存规格值 获取规格值id
                attr.getAttrValues().forEach(attrValue -> {
                    attrValue.setAttrId(attr.getAttrId());
                    int insert = attrValueMapper.insert(attrValue);
                    Assert.isZero(insert, Constant.SAVE_ATTR_VALUE);
                    //保存spu attr属性值
                    SpuAttrValue spuAttrValue = new SpuAttrValue()
                            .setSpuId(spu.getSpuId())
                            .setAttrId(attr.getAttrId())
                            .setAttrValueId(attrValue.getAttrValueId())
                            .setAttrName(attr.getName())
                            .setAttrValueName(attrValue.getValue());
                    int i = spuAttrValueMapper.insert(spuAttrValue);
                    Assert.isZero(i, Constant.SAVE_SPU_ATTR_VALUE);


                    //保存spu sku attr 属性值
                    if (attr.getAttrType()==0) {
                        for (Sku sku : spu.getSkuList()) {
                            String[] attrs = sku.getAttrs().split(",");
                            for (String s : attrs) {
                                if (s.equals(attrValue.getAttrValueId().toString())) {
                                    SpuSkuAttrValue value = new SpuSkuAttrValue()
                                            .setSpuId(spu.getSpuId())
                                            .setAttrId(attr.getAttrId())
                                            .setAttrValueId(attrValue.getAttrValueId())
                                            .setAttrName(attr.getName())
                                            .setAttrValueName(attrValue.getValue())
                                            .setSkuId(sku.getSkuId());
                                    i = spuSkuAttrValueMapper.insert(value);
                                    Assert.isZero(i, Constant.SAVE_SPU_SKU_ATTR_VALUE);
                                }
                            }
                        }
                    }
                });
            });
        }
        return true;
    }

}
